package com.mls.guardian.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.File;

/**
 * Created by jl on 16/7/5.
 */
public class SvnUtil {

        private static Logger logger = LoggerFactory.getLogger(SvnUtil.class);

    /**
     * 通过不同的协议初始化版本库
     */
    public static void setupLibrary() {
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
    }


    public static String getUpdateShellString(String projectPath,String revisionAddress){
        String shellStr;
        if (!SVNWCUtil.isVersionedDirectory(new File(projectPath))) {
            shellStr =  "svn checkout " + revisionAddress + " " + projectPath;
        } else {
            //svn update -r 123456 获取制定版本号
            shellStr =  "svn update " + projectPath;
        }

        return shellStr;
    }


    public static String getLastVersion(String url, String userName, String password) {
        String version = "0";
        try {
//            SVNRepository svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
//            //身份验证
//            ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, password.toCharArray());
//            svnRepository.setAuthenticationManager(authManager);
//            return svnRepository.getLatestRevision();
            SVNClientManager clientManager = authSvn(url, userName, password);
            if (clientManager != null) {
                long versionLong = clientManager.getRepositoryPool().createRepository(SVNURL.parseURIEncoded(url), true).getLatestRevision();

                if(versionLong != 0){
                    version = String.valueOf(versionLong);
                }
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        return version;
    }


    /**
          * 验证登录svn
          *
          * @param svnRoot  项目根目录 http://svn.ambow.com/wlpt/bsp/trunk
          * @param userName 登陆名
          * @param password 密码
          * @return
          */
    public static SVNClientManager authSvn(String svnRoot, String userName, String password) {
        // 初始化版本库
        setupLibrary();

        // 创建库连接
        SVNRepository repository = null;
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnRoot));
        } catch (SVNException e) {
            logger.error(e.getMessage());
            return null;
        }

        // 身份验证
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, password.toCharArray());

        // 创建身份验证管理器
        repository.setAuthenticationManager(authManager);

        DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
        SVNClientManager clientManager = SVNClientManager.newInstance(options, authManager);

        return clientManager;
    }
//
}
